/*
 * jiyan
 */
package com.jiyan.errorcode.service;

import com.jiyan.core.entity.Page;
import com.jiyan.core.exceptions.BaseException;
import com.jiyan.core.exceptions.ErrorcodePoolException;
import com.jiyan.errorcode.dao.ErrorcodePoolDAO;
import com.jiyan.errorcode.entity.ErrorcodePoolState;
import com.jiyan.errorcode.entity.ErrorcodePool;
import com.jiyan.core.base.BaseDAO;
import com.jiyan.core.base.BaseSVImpl;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.HashMap;
import java.util.Date;
import java.util.Map;
import org.apache.ibatis.session.RowBounds;
import com.baidu.fsg.uid.UidGenerator;

@Service("errorcodePoolSV")
@Slf4j
public class ErrorcodePoolSVImpl extends BaseSVImpl<ErrorcodePool, Long> implements ErrorcodePoolSV {

    @Autowired
    private ErrorcodePoolDAO errorcodePoolDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return errorcodePoolDAO;
    }

    /**
    * 保存account对象
    *
    * @param entity 实体
    * @throws BaseException
    */
    @Override
    public void save(ErrorcodePool entity) throws BaseException {
        super.save(entity);
    }

    /**
     * 加载一个对象ErrorcodePool
     * @param id * @param errorCode 错误码 由人为定义并制定出来指定一定场景模块的错误编码
     * @return ErrorcodePool
     */
    @Override
    public ErrorcodePool load(java.lang.Long id,java.lang.String errorCode) {
           if(id==null&&errorCode==null){
                 throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
           }

            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("errorCode",errorCode);
            return errorcodePoolDAO.load(param);
    }
    /**
     * 加载一个对象ErrorcodePool 通过id
     * @param id 
     * @return ErrorcodePool
     */
    @Override
    public ErrorcodePool loadById(java.lang.Long id) {
            if(id==null){
                 throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            return errorcodePoolDAO.loadById(id);
    }

    /**
     * 加载一个对象ErrorcodePool 通过errorCode
     * @param errorCode 错误码 由人为定义并制定出来指定一定场景模块的错误编码
     * @return ErrorcodePool
     */
    @Override
    public ErrorcodePool loadByErrorCode(java.lang.String errorCode) {
            if(errorCode==null){
                 throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            return errorcodePoolDAO.loadByErrorCode(errorCode);
    }


    /**
    * 根据主键id,oldStates 共同更新 ErrorcodePool 的状态到newState状态
    *
    * @param id 
    * @param newState 新状态
    * @param oldStates 旧状态集合
    */
    @Override
    public void updateStateById(java.lang.Long id,ErrorcodePoolState newState,ErrorcodePoolState... oldStates){
        if(id==null){
            throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(newState==null){
            throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(oldStates==null){
            throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        errorcodePoolDAO.updateStateById(id,new Date(),newState.name(), states);
    }
    /**
    * 根据主键errorCode,oldStates 共同更新 ErrorcodePool 的状态到newState状态
    *
    * @param errorCode 错误码 由人为定义并制定出来指定一定场景模块的错误编码
    * @param newState 新状态
    * @param oldStates 旧状态集合
    */
    @Override
    public void updateStateByErrorCode(java.lang.String errorCode,ErrorcodePoolState newState,ErrorcodePoolState... oldStates){
        if(errorCode==null){
            throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(newState==null){
            throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(oldStates==null){
            throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        errorcodePoolDAO.updateStateByErrorCode(errorCode,new Date(),newState.name(), states);
    }

    /**
    * 根据主键id 更新 ErrorcodePool 的状态到另一个状态
    *
    * @param id 
    * @param state 状态
    */
    @Override
    public void updateById(java.lang.Long id,ErrorcodePoolState state){
            if(id==null){
               throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
          errorcodePoolDAO.updateById(id,state.name(),new Date());
    }
    /**
    * 根据主键errorCode 更新 ErrorcodePool 的状态到另一个状态
    *
    * @param errorCode 错误码 由人为定义并制定出来指定一定场景模块的错误编码
    * @param state 状态
    */
    @Override
    public void updateByErrorCode(java.lang.String errorCode,ErrorcodePoolState state){
            if(errorCode==null){
               throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
          errorcodePoolDAO.updateByErrorCode(errorCode,state.name(),new Date());
    }


    /**
     * 删除对象ErrorcodePool
     * @param id * @param errorCode 错误码 由人为定义并制定出来指定一定场景模块的错误编码
     * @return ErrorcodePool
     */
    @Override
    public void delete(java.lang.Long id,java.lang.String errorCode) {
            if(id==null&&errorCode==null){
                throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("errorCode",errorCode);
            errorcodePoolDAO.delete(param);
    }



    /**
    * 查询ErrorcodePool分页
    *
    * @param errorcodePool  对象
    * @param offset 查询开始行
    * @param limit  查询行数
    * @return List<ErrorcodePool>
    */
    @Override
    public List<ErrorcodePool> list(ErrorcodePool errorcodePool, int offset, int limit) {
            if (offset < 0) {
               offset = 0;
            }

            if (limit < 0) {
               limit = Page.limit;
            }

            Map<String, Object> map = null;
            if (errorcodePool != null) {
               map = JSON.parseObject(JSON.toJSONString(errorcodePool, SerializerFeature.WriteDateUseDateFormat));
            } else {
               map = new HashMap<>();
            }
        return errorcodePoolDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public  int count(ErrorcodePool errorcodePool){
        Map<String, Object> map = null;
        if (errorcodePool != null) {
           map = JSON.parseObject(JSON.toJSONString(errorcodePool, SerializerFeature.WriteDateUseDateFormat));
        } else {
           map = new HashMap<>();
        }
       return errorcodePoolDAO.count(map);
     }

    /**
     * 查询ErrorcodePool分页
     *
     * @param id  * @param errorCode  错误码 由人为定义并制定出来指定一定场景模块的错误编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<ErrorcodePool>
     */
    @Override
    public List<ErrorcodePool> list(java.lang.Long id,java.lang.String errorCode,int offset, int limit) {
            if (offset < 0) {
              offset = 0;
            }

            if (limit < 0) {
              limit = Page.limit;
            }
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("errorCode",errorCode);
            return errorcodePoolDAO.list(param,new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Long id,java.lang.String errorCode) {
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("errorCode",errorCode);
            return errorcodePoolDAO.count(param);
    }

}
